<?php
/**
 *APINAME:
 *Developer:
 *Complete:
 */

namespace app\index\controller;


use app\index\common\BaseController;
use app\index\model\Option;
use app\index\model\Project;
use app\index\model\Record;
use think\facade\Request;

class Detail extends BaseController
{
    /**
     * 返回详情页所有信息
     * @param Project $project
     * @param Option $option
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detailPage(Project $project , Option $option){
        $proId = Request::get('id');
        $data = $project
            ->field(['*',"FROM_UNIXTIME(start_time,'%Y-%m-%d %H:%i:%s') as start_time","FROM_UNIXTIME(end_time,'%Y-%m-%d %H:%i:%s') as end_time"])
            ->where('id','=',$proId)->find();
        $data['userName'] = $data->user()->find()['userName'];
        $optMsg = $option->where('project_id','=',$proId)->field(['id as optId','content'])->select();
        $listAll = array();
        foreach ($optMsg as $key => $value){
            $list = ['optId' => $optMsg[$key]['optId'] , 'content' => $optMsg[$key]['content'] , 'count' => count($value->record()->where('option_id','=',$optMsg[$key]['optId'])->select())];
            array_push($listAll,$list);
        }
        $data['optMsg'] = $listAll;
        return $this->returnJson(self::OK,'返回项目所有信息',$data);
    }

    /**
     * 查看用户已投选项
     * @param Record $record
     * @param Option $option
     * @return \think\response\Json
     */
    public function alreadyThrow(){
        $result = $this->already($this->getUserId(),Request::get('id'));
        return $this->returnJson(self::OK,'查询用户已投选项成功',$result);
    }

    /**
     * 用户投票记录接口
     * @param Project $project
     * @param Record $record
     * @param Option $option
     * @return \think\response\Json
     */
    public function userThrow(Project $project , Record $record , Option $option){
        $userId = $this->getUserId();   //用户id
        $data = Request::param();       //获取选项内容和项目id
        $poll_number = $project->where('id','=',$data['id'])->find()['poll_number'];            //查找项目投票数
        $already = $this->already($userId,$data['id']);     //获取用户已经投票的项目
        if (count($already) < $poll_number){
            $record->insert(['user_id' => $userId,'option_id' => $data['optId']]);
            return $this->returnJson(self::OK,'投票成功');
        }
        return $this->returnJson(self::TICKET,'票数不够');
    }

    /**
     * 查询用户已经投票记录
     * @param $userId
     * @param $project_id
     * @return Option[]|\think\Collection
     */
    function already($userId,$project_id){
        $opt = Record::field('option_id')->where('user_id','=',$userId)->select();
        $optId = '';
        foreach ($opt as $key => $value){
            $optId .= $opt[$key]['option_id'] . ',';
        }
        $optId = rtrim($optId,',');
        return Option::where('project_id' ,'=',$project_id)->whereIn('id',$optId)->field('content')->select();
    }
}